var paginationPageSize = 20;
var paginationCurrentPage = 1;
var paginationTotal;
$(function () {
    $("#tip").dialog({
        autoOpen:false,
        modal:true,
        show:"slide"
    });

    if ($("[name=order]").val() == '0') {
        $("#tip").dialog("open");
    }
    $(".info-header").height($(".info-header-content").height() + 70);
    $(".tab-li").click(function () {
        $(".tab-li-click").removeClass("tab-li-click");
        $(this).addClass("tab-li-click");
    });

    $(".tab-li:eq(3)").click(function () {
        var orders_code=$("[name=orders_code]").val() ;
        if (orders_code== '') {
            $(".info-content-body").html("<div style='width:100%;text-align:center'>No Result</div>");
            return;
        }
        loading();
        $.ajax({
            url:"/merchant/orders/get-order-ship/orders_code/" + orders_code + "/" + new Date(),
            async:false,
            dataType:"json",
            success:function (json) {
                if (json.ask) {
                    var html = '';
                    var html = '<div class="row height25 row-header"><div class="perw15 cell">TrackingNo</div><div class="perw15 cell">TrackingNumber</div><div class="per10 cell">Weight</div><div class="perw15 cell">Declared Value</div><div class="per10" style="float:left;text-align: center">Shipping Fee</div></div>';
                    $.each(json.result, function (k, v) {
                        var ref_tracking_number=(v.ref_tracking_number!='' && v.ref_tracking_number!=null)?v.ref_tracking_number:'&nbsp;';
                        html += '<div class="line1">';
                        html += '<div class="row height110"><div class="perw15  cell2" title="">' + v.tracking_number + '</div><div class="perw15  cell2" title="">' + ref_tracking_number + '</div><div class="per10 cell2" title="">' + v.os_weight + '</div><div class="perw15  cell2" title="">' + v.os_declared_value + '</div><div class="per10" style="float:left;text-align: center" title="">' + v.os_shipping_fee + '</div></div>'
                        html += '</div>';
                    });
                    $(".info-content-body").html(html);
                } else {
                    $(".info-content-body").html("<div style='width:100%;text-align:center'>No Result</div>");
                }
                closeLoading();
            }
        });
    });

    $(".tab-li:eq(2)").click(function () {
        if ($("[name=tracking_number]").val() == '') {
            $(".info-content-body").html("<div style='width:100%;text-align:center'>No Result</div>");
            return;
        }
        loading();
        $.ajax({
            url:"/merchant/orders/get-track-info/tracking_number/" + $("[name=tracking_number]").val() + "/" + new Date(),
            async:false,
            dataType:"json",
            success:function (json) {
                if (json.ask) {
                    var html = '<div class="row height25 row-header"><div class="per40  cell">Activity</div><div class="per30 cell">TrackDate</div><div class="per30" style="float:left;text-align: center">Location</div></div>';
                    $.each(json.result, function (k, v) {
                        if ((k + 1) % 2 == 1) {
                            html += '<div class="line1">';
                        } else {
                            html += '<div class="line2">';
                        }

                        html += '<div class="row height110"><div class="per40  cell2" title="">' + v.activity + '</div><div class="per30 cell2" title="">' + v.track_date + '</div><div class="per30 " style="float:left;text-align: center" title="">' + v.locations + '</div></div>'

                        html += '</div>';
                    });
                    $(".info-content-body").html(html);
                } else {
                    $(".info-content-body").html("<div style='width:100%;text-align:center'>No Result</div>");
                }
                closeLoading();
            }
        });
    });
    $(".tab-li:eq(1)").click(function () {
        loading();
        $.ajax({
            url:"/merchant/orders/get-order-log/orders_code/" + $("[name=orders_code]").val() + "/page/" + paginationCurrentPage + "/pageSize/" + paginationPageSize + "/" + new Date(),
            async:false,
            dataType:"json",
            success:function (json) {
                if (json.ask) {
                    var html = '<div class="row height25 row-header"><div class="per10  cell">OrdersCode</div><div class="per10 cell">Type</div><div class="per10 cell">HistoryStatus</div><div class="per10 cell">CreateDate</div><div class="per12 cell">Comments</div><div class="per15">IP</div></div>';
                    $.each(json.results, function (k, v) {
                        if ((k + 1) % 2 == 1) {
                            html += '<div class="line1">';
                        } else {
                            html += '<div class="line2">';
                        }

                        html += '<div class="row height110"><div class="per10  cell2" title="">' + v.orders_code + '</div><div class="per10 cell2" title="">' + v.osh_type + '</div><div class="per10 cell2" title="">' + v.osh_statu + '</div><div class="per10 height110 cell2" title="">' + v.osh_create_date + '</div><div class="per11 cell2" title="">' + v.osh_comments + '</div><div class="per15" title="">' + v.osh_ip + '</div></div>'

                        html += '</div>';
                    });
                    html += '<div class="pagination" style="padding-top:10px" title="log"></div>';
                    $(".info-content-body").html(html);


                    $(".info-content-body .row").each(function (i) {
                        var h = $(this).height();//alert(h);
                        $(".cell2", $(this)).height(h);

                    });


                    $(".pagination").pagination(json.total, {
                        callback:function (page_id) {
                            paginationCurrentPage = page_id + 1;
                            paginationTotal = json.total;
                            $(".tab-li:eq(1)").click();
                        },
                        first_text:'First',
                        prev_text:'< Previous',
                        next_text:'Next >',
                        last_text:'Last',
                        perpage_text:'PerPage',
                        total_text:'Total',
                        items_per_page:paginationPageSize,
                        num_display_entries:6,
                        current_page:paginationCurrentPage - 1,
                        num_edge_entries:2
                    });

                } else {
                    $(".info-content-body").html("<div style='width:100%;text-align:center'>No Result</div>");
                }
                closeLoading();
            }
        });
    });
    $(".tab-li:eq(0)").click(
        function () {

            loading();
            $.ajax({
                url:"/merchant/orders/get-product-info/orders_code/" + $("[name=orders_code]").val() + "/page/" + paginationCurrentPage + "/pageSize/" + paginationPageSize + "/" + new Date(),
                async:false,
                dataType:"json",
                success:function (json) {
                    if (json.ask) {
                        var html = '<div class="row height25 row-header"><div class="per20  cell">Images</div><div class="per23 cell">ProductSku</div><div class="per23 cell">ProductName</div><div class="per10 cell">DeclaredValue</div><div class="per22 cell">Quantity</div><div class="per15">Sum</div></div>';
                        $.each(json.results, function (k, v) {
                            if ((k + 1) % 2 == 1) {
                                html += '<div class="line1">';
                            } else {
                                html += '<div class="line2">';
                            }

                            html += '<div class="row height110"><div class="per20  cell3" title=""><img style="width:100px;height:70px;" src="' + v.product_img + '"/></div><div class="per23 cell3" title="">' + v.product_sku + '</div><div class="per23 cell3" title="">' + v.product_title + '</div><div class="per10 height110 cell3" title="">' + v.op_declared_value + '</div><div class="per22 cell3" title="">' + v.op_quantity + '</div><div class="per25" title="">' + v.op_declared_value * v.op_quantity + '</div></div>'

                            html += '</div>';
                        });
                        html += '<div class="pagination" style="padding-top:10px" title="log"></div>';
                        $(".info-content-body").html(html);
                        $(".info-content-body .row").each(function (i) {
                            var h = $(this).height();//alert(h);
                            $(".cell2", $(this)).height(h);

                        });

                        $(".pagination").pagination(json.total, {
                            callback:function (page_id) {
                                paginationCurrentPage = page_id + 1;
                                paginationTotal = json.total;
                                $(".tab-li:eq(0)").click();
                            },
                            first_text:'First',
                            prev_text:'< Previous',
                            next_text:'Next >',
                            last_text:'Last',
                            perpage_text:'PerPage',
                            total_text:'Total',
                            items_per_page:paginationPageSize,
                            num_display_entries:6,
                            current_page:paginationCurrentPage - 1,
                            num_edge_entries:2
                        });

                    } else {
                        $(".info-content-body").html("<div style='width:100%;text-align:center'>No Result</div>");
                    }
                    closeLoading();
                }
            });

        }).click();
});


function changePageSize(obj) {
    paginationPageSize = $(obj).val();
    if (paginationPageSize <= 0) {
        paginationPageSize = 20;
    }

    if (Math.ceil(paginationTotal / paginationPageSize) <= paginationCurrentPage) {
        paginationCurrentPage = Math.ceil(paginationTotal / paginationPageSize);
    }
    if (paginationCurrentPage == 0) {
        paginationCurrentPage = 1;
    }
    eval($(".pagination").attr("title") + '()');
}
function log() {
    $(".tab-li:eq(0)").click();
}